<?php
/**
 * 获得用户信息动作类
 * @author yaoli<yaoli@pateo.com.cn>
 * @version 1.0 GetUser add by yaoli<yaoli@pateo.com.cn> 
 */
class roadbook extends ActionBase {
	
	public function init() {
	 	$this->date = parent::$data;
		$this->date ['page'] = (isset ( $_REQUEST ['page'] )) ? ($_REQUEST ['page'] - 1) : 0;
	
	}
	
	
	/**
	 * 动作
	 *
	 * @see AActionBase::action()
	 */
	
	// 书
	public function action() {
		
		$this->date ['roadbook'] = load_model ( 'roadbook' )->getRoadbook ( $this->date ['page'] ,array('action!'=>'delete'));
		
		$this->date ['count'] = load_model ( 'roadbook' )->getRcount (array('action!'=>'delete') );
		
		$this->date ['pagecontent'] = fenye ( url ( 'roadbook/action' ) . "&page=", $this->date ['page'] + 1, $this->date ['count'], PERPAGE );
		
		
		$this->template ();
		$this->template->assign ( $this->date );
		$this->template->display ( 'roadbook.html' );
	}
	
	// 添加路书
	public function add() {
		if (isset ( $_REQUEST ['data'] )) {
			
			if($_REQUEST['data']['destination'] && $_REQUEST['data']['depart']){
				unset($_REQUEST ['data']['departname']);
				unset($_REQUEST ['data']['destinationname']);
				
				$URL= url('roadbook/add')."&error=1";
				
				if(load_model('way')->getOneTable(array('way_id'=>$_REQUEST['data']['destination']))){
					if(load_model('way')->getOneTable(array('way_id'=>$_REQUEST['data']['depart']))){
						$_REQUEST ['data']['action'] = 'create';
						$_REQUEST ['data'] ['create_time'] = time ();
						load_model ( 'roadbook' )->replaceTable ( array ('data' => $_REQUEST ['data'] ) );
						$URL = url ( 'roadbook/index' );
						
					}
				}
				
				header ( "Location: $URL" );
				exit ();
				
			
			}
		} else {
			$this->date['alert']['str']='';
			$this->date['alert']['color']='';
			
			$this->date['error']=0;
			if(isset($_REQUEST['error'])&&$_REQUEST['error']==1){
				$this->date['alert']['str']='出发地或目的地不存在！';
				$this->date['alert']['color']='red';
			}
			$this->template ();
			$this->template->assign ( $this->date );
			$this->template->display ( 'roadbook_add.html' );
		}
	}
	
	// 修改路书
	public function modify() {
		
		if (isset ( $_REQUEST ['data'] )) {
			
			
			
			if($_REQUEST['data']['destination'] && $_REQUEST['data']['depart']){
				
				unset($_REQUEST ['data']['departname']);
				unset($_REQUEST ['data']['destinationname']);
			
			
				$URL= url('roadbook/modify',array('roadbook_id'=>$_REQUEST['data']['roadbook_id'],'error'=>1));
			
				if(load_model('way')->getOneTable(array('way_id'=>$_REQUEST['data']['destination']))){
					if(load_model('way')->getOneTable(array('way_id'=>$_REQUEST['data']['depart']))){
						$_REQUEST ['data']['action'] = 'update';
						$_REQUEST ['data'] ['create_time'] = time ();
						load_model ( 'roadbook' )->replaceTable ( array ('data' => $_REQUEST ['data'] ) );
						$URL= url('roadbook/modify',array('roadbook_id'=>$_REQUEST['data']['roadbook_id'],'suc'=>1));
			
					}
				}
					
			}
			header ( "Location: $URL" );
			exit ();
		} elseif (isset ( $_REQUEST ['roadbook_id'] )) {
			
			$this->date['alert']['str']='';
			$this->date['alert']['color']='';
				
			$this->date['error']=0;
			if(isset($_REQUEST['error'])&&$_REQUEST['error']==1){
				$this->date['alert']['str']='出发地或目的地不存在！';
				$this->date['alert']['color']='red';
			}elseif(isset($_REQUEST['suc'])&&$_REQUEST['suc']==1){
				$this->date['alert']['str']='操作成功！';
				$this->date['alert']['color']='green';
			}
			
			$roadbook = load_model ( 'roadbook' )->getTable ( array ('roadbook_id' => $_REQUEST ['roadbook_id'] ) );
			$this->date ['roadbook'] = $roadbook [0];
			$this->template ();
			$this->template->assign ( $this->date );
			$this->template->display ( 'roadbook_modify.html' );
		} else {
			$this->template ();
			$this->template->assign ( $this->date );
			$this->template->display ( 'roadbook_add.html' );
		}
	}
	
	// 除路书
	public function del() {
		load_model ( 'roadbook' )->delTable ( array ('roadbook_id' => $_REQUEST ['roadbook_id'] ) );
		load_model ( 'roadbook_way' )->delTable ( array ('roadbook_id' => $_REQUEST ['roadbook_id'] ) );
		load_model ( 'roadbook_point' )->delTable ( array ('roadbook_id' => $_REQUEST ['roadbook_id'] ) );
		
		/* $pics = load_model('pic')->getTable ( array ('type'=>'roadbook','id' => $_REQUEST ['roadbook_id'] ),100 );
		
		 load_model('pic')->delTable ( array ('type'=>'roadbook','id' => $_REQUEST ['roadbook_id'] ));
		foreach($pics as $key=>$value){
			@unlink('.'.$value['url']);	
		} */
		$URL = $_SERVER ['HTTP_REFERER'];
		header ( "Location: $URL" );
		exit ();
	}
	
	#路书添加途径地
	public function way(){
	
	$this->date['roadbook_id']=$_REQUEST['roadbook_id'];
	
	$this->date['alert']['str']='';
	$this->date['alert']['color']='';
		
		if(isset($this->date['roadbook_id'])){
			if(isset($_REQUEST['data']['way_id'])){
					$count1 = load_model('roadbook_way')->getRcount(array('roadbook_id'=>$_REQUEST['roadbook_id'],'way_id'=>$_REQUEST['data']['way_id']));
				        $count = load_model('way')->getRcount(array('way_id'=>$_REQUEST['data']['way_id']));
					if($count!=1){
						$this->date['alert']['str']='该途径地不存在！';
						$this->date['alert']['color']='red';
					}elseif($count1>0){
						$this->date['alert']['str']='该途径地已存在！';
						$this->date['alert']['color']='red';
					}else{
						$count1 = load_model('roadbook_way')->
						replaceTable(array('roadbook_id'=>$_REQUEST['roadbook_id'],'way_id'=>$_REQUEST['data']['way_id'],'order'=>$_REQUEST['data']['order']));
						if($count1==1){
							$this->date['alert']['str']='该途径地已添加！';
							$this->date['alert']['color']='green';
							$URL = $_SERVER ['HTTP_REFERER'];
             
						}else{
							$this->date['alert']['str']='未知错误添加失败！';
							$this->date['alert']['color']='red';
						}
					}
			}
			
		 	$way = load_model('roadbook_way')->getRoadWay(array('wr.roadbook_id'=>$_REQUEST['roadbook_id']));
			
			$this->date['way'] = $way;
		}
		$this->template ();
		$this->template->assign ( $this->date );
		$this->template->display ( 'roadbook_way.html' );
	}
	
	#路书添加兴趣点
	public function point(){
		$this->date['roadbook_id']=$_REQUEST['roadbook_id'];
		
		$this->date['alert']['str']='';
		$this->date['alert']['color']='';
		
		if(isset($this->date['roadbook_id'])){
			if(isset($_REQUEST['data']['point_id'])){
					$count1 = load_model('roadbook_point')->getRcount(array('roadbook_id'=>$_REQUEST['roadbook_id'],'point_id'=>$_REQUEST['data']['point_id']));
					$count = load_model('point')->getRcount(array('point_id'=>$_REQUEST['data']['point_id']));
					if($count!=1){
						$this->date['alert']['str']='该兴趣点不存在！';
						$this->date['alert']['color']='red';
					}elseif($count1>0){
						$this->date['alert']['str']='该兴趣点已存在！';
						$this->date['alert']['color']='red';
					}else{
						$count1 = load_model('roadbook_point')->
						replaceTable(array('roadbook_id'=>$_REQUEST['roadbook_id'],'point_id'=>$_REQUEST['data']['point_id'],'order'=>$_REQUEST['data']['order']));
						if($count1==1){
							$this->date['alert']['str']='该兴趣点已添加！';
							$this->date['alert']['color']='green';
							$URL = $_SERVER ['HTTP_REFERER'];
						}else{
							$this->date['alert']['str']='未知错误添加失败！';
							$this->date['alert']['color']='red';
						}
					}
			}
			
			$this->date['point'] = load_model('roadbook_point')->getRoadPoint(array('wr.roadbook_id'=>$_REQUEST['roadbook_id']));
		}
		
		$this->template ();
		$this->template->assign ( $this->date );
		$this->template->display ( 'roadbook_point.html' );
	}
	
	
	

}
